home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / DefaultCellEditor.java < prev    next >
Text File  |  1998-06-30  |  10KB  |  364 lines

  1. /*
  2.  * @(#)DefaultCellEditor.java    1.25 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.awt.Component;
  24. import java.awt.event.*;
  25. import java.awt.AWTEvent;
  26. import java.lang.Boolean;
  27. import com.sun.java.swing.table.*;
  28. import com.sun.java.swing.event.*;
  29. import java.util.EventObject;
  30. import com.sun.java.swing.*;
  31. import com.sun.java.swing.tree.*;
  32. import java.io.Serializable;
  33.  
  34. /**
  35.  * The default editor for table and tree cells.
  36.  * <p>
  37.  * Warning: serialized objects of this class will not be compatible with
  38.  * future swing releases.  The current serialization support is appropriate 
  39.  * for short term storage or RMI between Swing1.0 applications.  It will
  40.  * not be possible to load serialized Swing1.0 objects with future releases
  41.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  42.  * baseline for the serialized form of Swing objects.
  43.  *
  44.  * @version 1.25 02/02/98
  45.  * @author Alan Chung
  46.  */
  47.  
  48. public class DefaultCellEditor implements TableCellEditor, TreeCellEditor,
  49.                       Serializable {
  50. //
  51. //  Instance Variables
  52. //
  53.  
  54.     /** Event listeners */
  55.     protected EventListenerList listenerList = new EventListenerList();
  56.     transient protected ChangeEvent changeEvent = null;
  57.  
  58.     protected JComponent editorComponent;
  59.     protected EditorDelegate delegate;
  60.     protected int clickCountToStart = 1;
  61.  
  62. //
  63. //  Constructors
  64. //
  65.  
  66.     public DefaultCellEditor(JTextField x) {
  67.         this.editorComponent = x;
  68.     this.clickCountToStart = 2;
  69.         this.delegate = new EditorDelegate() {
  70.             public void setValue(Object x) {
  71.                 super.setValue(x);
  72.         if (x != null)
  73.             ((JTextField)editorComponent).setText(x.toString());
  74.         else
  75.             ((JTextField)editorComponent).setText("");
  76.             }
  77.  
  78.         public Object getCellEditorValue() {
  79.         return ((JTextField)editorComponent).getText();
  80.         }
  81.  
  82.         public boolean startCellEditing(EventObject anEvent) {
  83.         if(anEvent == null)
  84.             editorComponent.requestFocus();
  85.         return true;
  86.         }
  87.  
  88.         public boolean stopCellEditing() {
  89.         return true;
  90.         }
  91.         };
  92.     ((JTextField)editorComponent).addActionListener(delegate);
  93.     }
  94.  
  95.     public DefaultCellEditor(JCheckBox x) {
  96.         this.editorComponent = x;
  97.         this.delegate = new EditorDelegate() {
  98.             public void setValue(Object x) {
  99.                 super.setValue(x);
  100.  
  101.         // Try my best to do the right thing with x
  102.         if (x instanceof Boolean) {
  103.             ((JCheckBox)editorComponent).setSelected(((Boolean)x).booleanValue());
  104.         }
  105.         else if (x instanceof String) {
  106.             Boolean b = new Boolean((String)x);
  107.             ((JCheckBox)editorComponent).setSelected(b.booleanValue());
  108.         }
  109.         else {
  110.             ((JCheckBox)editorComponent).setSelected(false);
  111.         }
  112.             }
  113.  
  114.         public Object getCellEditorValue() {
  115.         return new Boolean(((JCheckBox)editorComponent).isSelected());
  116.         }
  117.  
  118.         public boolean startCellEditing(EventObject anEvent) {
  119.         // PENDING(alan)
  120.         if (anEvent instanceof AWTEvent) {
  121.             return true;
  122.         }
  123.         return false;
  124.         }
  125.  
  126.         public boolean stopCellEditing() {
  127.         return true;
  128.         }
  129.         };
  130.     ((JCheckBox)editorComponent).addActionListener(delegate);
  131.     }
  132.  
  133.     public DefaultCellEditor(JComboBox x) {
  134.         this.editorComponent = x;
  135.         this.delegate = new EditorDelegate() {
  136.             public void setValue(Object x) {
  137.                 super.setValue(x);
  138.         ((JComboBox)editorComponent).setSelectedItem(x);
  139.             }
  140.  
  141.         public Object getCellEditorValue() {
  142.         return ((JComboBox)editorComponent).getSelectedItem();
  143.         }
  144.  
  145.         public boolean startCellEditing(EventObject anEvent) {
  146.         if (anEvent instanceof AWTEvent) {
  147.             return true;
  148.         }
  149.         return false;
  150.         }
  151.  
  152.         public boolean stopCellEditing() {
  153.         return true;
  154.         }
  155.         };
  156.     ((JComboBox)editorComponent).addItemListener(delegate);
  157.     }
  158.  
  159.     public Component getComponent() {
  160.     return editorComponent;
  161.     }
  162.  
  163. //
  164. //  Modifying
  165. //
  166.  
  167.     public void setClickCountToStart(int count) {
  168.     clickCountToStart = count;
  169.     }
  170.  
  171.     /**
  172.      *  clickCountToStart controls the number of clicks required to start
  173.      *  editing if the event passed to isCellEditable() or startCellEditing() is
  174.      *  a MouseEvent.  For example, by default the clickCountToStart for
  175.      *  a JTextField is set to 2, so in a JTable the user will need to
  176.      *  double click to begin editing a cell.
  177.      */
  178.     public int getClickCountToStart() {
  179.     return clickCountToStart;
  180.     }
  181.  
  182. //
  183. //  Implementing the CellEditor Interface
  184. //
  185.  
  186.     public Object getCellEditorValue() {
  187.         return delegate.getCellEditorValue();
  188.     }
  189.  
  190.     public boolean isCellEditable(EventObject anEvent) {
  191.     if (anEvent instanceof MouseEvent) {
  192.         if (((MouseEvent)anEvent).getClickCount() < clickCountToStart)
  193.         return false;
  194.     }
  195.     return delegate.isCellEditable(anEvent);
  196.     }
  197.     
  198.     public boolean shouldSelectCell(EventObject anEvent) {
  199.     boolean         retValue = true;
  200.  
  201.     if (this.isCellEditable(anEvent)) {
  202.         if (anEvent == null || ((MouseEvent)anEvent).getClickCount() >= 
  203.         clickCountToStart)
  204.         retValue = delegate.startCellEditing(anEvent);
  205.     }
  206.  
  207.     // By default we want the cell the be selected so
  208.     // we return true
  209.     return retValue;
  210.     }
  211.  
  212.     public boolean stopCellEditing() {
  213.     boolean stopped = delegate.stopCellEditing();
  214.  
  215.     if (stopped) {
  216.         fireEditingStopped();
  217.     }
  218.     
  219.     return stopped;
  220.     }
  221.  
  222.     public void cancelCellEditing() {
  223.     delegate.cancelCellEditing();
  224.     fireEditingCanceled();
  225.     }
  226.  
  227. //
  228. //  Handle the event listener bookkeeping
  229. //
  230.     public void addCellEditorListener(CellEditorListener l) {
  231.     listenerList.add(CellEditorListener.class, l);
  232.     }
  233.  
  234.     public void removeCellEditorListener(CellEditorListener l) {
  235.     listenerList.remove(CellEditorListener.class, l);
  236.     }
  237.  
  238.     /*
  239.      * Notify all listeners that have registered interest for
  240.      * notification on this event type.  The event instance 
  241.      * is lazily created using the parameters passed into 
  242.      * the fire method.
  243.      * @see EventListenerList
  244.      */
  245.     protected void fireEditingStopped() {
  246.     // Guaranteed to return a non-null array
  247.     Object[] listeners = listenerList.getListenerList();
  248.     // Process the listeners last to first, notifying
  249.     // those that are interested in this event
  250.     for (int i = listeners.length-2; i>=0; i-=2) {
  251.         if (listeners[i]==CellEditorListener.class) {
  252.         // Lazily create the event:
  253.         if (changeEvent == null)
  254.             changeEvent = new ChangeEvent(this);
  255.         ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
  256.         }           
  257.     }
  258.     }
  259.  
  260.     /*
  261.      * Notify all listeners that have registered interest for
  262.      * notification on this event type.  The event instance 
  263.      * is lazily created using the parameters passed into 
  264.      * the fire method.
  265.      * @see EventListenerList
  266.      */
  267.     protected void fireEditingCanceled() {
  268.     // Guaranteed to return a non-null array
  269.     Object[] listeners = listenerList.getListenerList();
  270.     // Process the listeners last to first, notifying
  271.     // those that are interested in this event
  272.     for (int i = listeners.length-2; i>=0; i-=2) {
  273.         if (listeners[i]==CellEditorListener.class) {
  274.         // Lazily create the event:
  275.         if (changeEvent == null)
  276.             changeEvent = new ChangeEvent(this);
  277.         ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
  278.         }           
  279.     }
  280.     }
  281.  
  282. //
  283. //  Implementing the TreeCellEditor Interface
  284. //
  285.  
  286.     public Component getTreeCellEditorComponent(JTree tree, Object value,
  287.                         boolean isSelected,
  288.                         boolean expanded,
  289.                         boolean leaf, int row) {
  290.     String         stringValue = tree.convertValueToText(value, isSelected,
  291.                         expanded, leaf, row, false);
  292.  
  293.     delegate.setValue(stringValue);
  294.     return editorComponent;
  295.     }
  296.  
  297. //
  298. //  Implementing the CellEditor Interface
  299. //
  300.  
  301.     public Component getTableCellEditorComponent(JTable table, Object value,
  302.                          boolean isSelected,
  303.                          int row, int column) {
  304.  
  305.     // Modify component colors to reflect selection state
  306.     // PENDING(alan)
  307.     /*if (isSelected) {
  308.         component.setBackground(selectedBackgroundColor);
  309.         component.setForeground(selectedForegroundColor);
  310.     }
  311.     else {
  312.         component.setBackground(backgroundColor);
  313.         component.setForeground(foregroundColor);
  314.     }*/
  315.  
  316.         delegate.setValue(value);
  317.     return editorComponent;
  318.     }
  319.  
  320.  
  321. //
  322. //  Protected EditorDelegate class
  323. //
  324.  
  325.     protected class EditorDelegate implements ActionListener, ItemListener, Serializable {
  326.  
  327.         protected Object value;
  328.  
  329.         public Object getCellEditorValue() {
  330.             return value;
  331.         }
  332.  
  333.         public void setValue(Object x) {
  334.             this.value = x;
  335.         }
  336.  
  337.         public boolean isCellEditable(EventObject anEvent) {
  338.         return true;
  339.     }
  340.  
  341.         public boolean startCellEditing(EventObject anEvent) {
  342.         return true;
  343.     }
  344.  
  345.         public boolean stopCellEditing() {
  346.         return true;
  347.     }
  348.  
  349.         public void cancelCellEditing() {
  350.     }
  351.  
  352.     // Implementing ActionListener interface
  353.         public void actionPerformed(ActionEvent e) {
  354.         fireEditingStopped();
  355.     }
  356.  
  357.     // Implementing ItemListener interface
  358.         public void itemStateChanged(ItemEvent e) {
  359.         fireEditingStopped();
  360.     }
  361.     }
  362.  
  363. } // End of class JCellEditor
  364.